<?php
/*
 * @Author: he4966
 */

namespace addons\translq\controller;

use think\addons\Controller;
use think\queue\Job;
use think\Db;
use app\common\library\LangCom;
use think\Queue;

class TranslJob extends Controller
{
    public function fire(Job $job, $data)
    {
        var_dump($data);
        // 任务执行完毕，删除任务
        $job->delete();
    }



    public function FanyiOtherDataItem(Job $job,$data){
        var_dump($data);
        $table_name = $where['table_name'] = $data['table_name'];
        $id = $where['table_id'] =  $data['id'];
        $lang = $where['lang'] = $data['lang'];


        $translq_queue_id = $where['translq_queue_id']= $data['translq_queue_id'];
      
        $res = Db::name('translq_subjob')->where($where)->find();

        if(!$res){
            $where['status'] = 'ing';
            $where['createtime'] = time();
            $translq_subjob_id = Db::name('translq_subjob')->insertGetId($where);
            (new LangCom)->fanyiOtherDataItem($table_name,$id,$lang);
            $runRes = Db::table($table_name)->where('copy_id',$id)->where('lang',$lang)->find();
            if($runRes){
                Db::name('translq_subjob')->where('id',$translq_subjob_id)->update(['status'=>'end','endtime'=>time(),'run_sec'=>(time() - $where['createtime'])]);
                $ingNum = Db::name('translq_subjob')->where('translq_queue_id',$translq_queue_id)->where('status','in',['wait','ing'])->count();
                if($ingNum == 0){
                    //状态:wait=待开始,ing=进行中,end=完成,stop=强制停止
                    $translMain = Db::name('translq_queue')->where('id',$translq_queue_id)->find();
                    Db::name('translq_queue')->where('id',$translq_queue_id)->update(['status'=>'end','endtime'=>time(),'run_sec'=>(time() - $translMain['createtime'])]);
                }
               //删除当前任务
               $job->delete();
            }else{
                var_dump('没有找到数据',$table_name,$id,$lang);
            }
        }
        $job->delete();
    
    }

    

    public function UpdateOtherDataItem(Job $job,$data){
        var_dump($data);
        $table_name = $where['table_name'] = $data['table_name'];
        $id = $where['table_id'] =  $data['id'];
        $lang = $where['lang'] = $data['lang'];


        $translq_queue_id = $where['translq_queue_id']= $data['translq_queue_id'];


        $res = Db::name('translq_subjob')->where($where)->find();
        if(!$res){
            $where['status'] = 'ing';
            $where['createtime'] = time();
            $translq_subjob_id = Db::name('translq_subjob')->insertGetId($where);
            (new LangCom)->updateOtherDataItem($table_name,$id,$lang);
            $runRes = Db::table($table_name)->where('copy_id',$id)->where('lang',$lang)->find();
            if($runRes){
                Db::name('translq_subjob')->where('id',$translq_subjob_id)->update(['status'=>'end','endtime'=>time(),'run_sec'=>(time() - $where['createtime'])]);
                $ingNum = Db::name('translq_subjob')->where('translq_queue_id',$translq_queue_id)->where('status','in',['wait','ing'])->count();
                if($ingNum == 0){
                    //状态:wait=待开始,ing=进行中,end=完成,stop=强制停止
                    $translMain = Db::name('translq_queue')->where('id',$translq_queue_id)->find();
                    Db::name('translq_queue')->where('id',$translq_queue_id)->update(['status'=>'end','endtime'=>time(),'run_sec'=>(time() - $translMain['createtime'])]);
                }
               //删除当前任务
               $job->delete();
            }else{
                var_dump('没有找到数据',$table_name,$id,$lang);
            }
        }
        $job->delete();
    }

}
